package com.app.couponsharelife.ui.main.special
import android.content.Intent
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.appbar.AppBarLayout
import com.gyf.barlibrary.ImmersionBar
import com.app.couponsharelife.R
import com.app.couponsharelife.base.BaseActivity
import com.app.couponsharelife.data.bean.BrandSpecialItemBean
import com.app.couponsharelife.data.bean.RemoteResultBean
import com.app.couponsharelife.data.bean.TaobaoGoodBean
import com.app.couponsharelife.data.remote.retrofit.RetrofitWorker
import com.app.couponsharelife.data.remote.retrofit.exception.OtherException
import com.app.couponsharelife.manage.Contacts
import com.app.couponsharelife.ui.main.home.good.GoodDetailActivity
import com.app.couponsharelife.utils.DataUtils
import com.app.couponsharelife.utils.LoadUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_brand_special_list.*
import kotlinx.android.synthetic.main.rvitem_newmen_free.view.*
import java.util.HashMap

/**
 *Created by xct on 2019/5/18
 *Description:品牌专场二级列表页面
 */
class BrandSpecialListActivity:BaseActivity() {
    private var title=""
    private var minID=""
    private var immersionBar: ImmersionBar?=null
    private var mCurrentState=0
    private var goodsList= arrayListOf<TaobaoGoodBean>()
    private var adapter:MyAdapter?=null
    override val viewLayoutId: Int
        get() = R.layout.activity_brand_special_list

    override fun initViews() {
        immersionBar= ImmersionBar.with(this)
        immersionBar!!.statusBarDarkFont(false).init()
        ivClose.setOnClickListener { finish() }
        title=intent.getStringExtra("Name")?:""
        minID=intent.getStringExtra("Id")?:""
        if (minID.isEmpty()){
            showToastMsg("数据出错了！")
        }
        tvTitle.text=title
        appbarlayout.addOnOffsetChangedListener(object : AppBarLayout.OnOffsetChangedListener{
            override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
                if (verticalOffset == 0) {
                    if (mCurrentState!=0){
                        //Log.e("eeee","展开了")
                        immersionBar!!.statusBarDarkFont(false).init()
                        ivClose.setImageResource(R.drawable.ic_back_white)
                        tvTitle.setTextColor(resources.getColor(R.color.white))
                    }
                    mCurrentState=0
                } else if (Math.abs(verticalOffset) >= appBarLayout!!.totalScrollRange) {
                    if (mCurrentState!=2){
                        //Log.e("eeee","折叠了")
                        immersionBar!!.statusBarDarkFont(true).init()
                        ivClose.setImageResource(R.drawable.back_gray)
                        tvTitle.setTextColor(resources.getColor(R.color.black))
                    }
                    mCurrentState=2
                } else {
                    mCurrentState=1
                }
            }
        })
        adapter=MyAdapter()
        rvGoods.layoutManager=LinearLayoutManager(this)
        rvGoods.adapter=adapter
        getData()
    }
    private fun getData(){
        showProgressDialog("加载中...")
        val map= HashMap<String,String>()
        map["id"]=minID
        RetrofitWorker.retrofitWorker.getSingleBrandInfor(map)
                .subscribeOn(Schedulers.io())
                .map { DataUtils().hdkData1(it) }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DisposableObserver<RemoteResultBean<BrandSpecialItemBean>>(){
                    override fun onComplete() {

                    }

                    override fun onNext(it: RemoteResultBean<BrandSpecialItemBean>) {
                        dismissProgressDialog()
                        if (it.code==200){
                            if (it.data != null && it.data!!.singleBrandInfor!=null
                                    &&it.data!!.singleBrandInfor!!.data!=null&&it.data!!.singleBrandInfor!!.data!!.isNotEmpty()
                                    &&it.data!!.singleBrandInfor!!.data!![0]!=null&&it.data!!.singleBrandInfor!!.data!![0]!!.items!=null
                                    &&it.data!!.singleBrandInfor!!.data!![0]!!.items!!.isNotEmpty()){
                                goodsList.clear()
                                val dataBean=it.data!!.singleBrandInfor!!.data[0]
                                goodsList.addAll(dataBean.datas)
                                val imagUrl= Contacts.NEWREMOTE_IMAGE_URL +it.data!!.image?:""
                                val imagLogo= dataBean.inside_logo?:""
                                LoadUtils().loadCommonImag(this@BrandSpecialListActivity,ivTopBanner,imagUrl,R.drawable.ic_app_place_holder3)
                                LoadUtils().loadCommonImag(this@BrandSpecialListActivity,ivShopLogo,imagLogo,R.drawable.ic_app_place_holder3)
                                tvContent.text=dataBean.introduce?:""
                                adapter!!.notifyDataSetChanged()
                            }else{
                                showToastMsg("暂无商品！")
                                finish()
                            }
                        }
                    }

                    override fun onError(e: Throwable) {
                        dismissProgressDialog()
                        if (e is OtherException){
                            showToastMsg(e.msg)
                        }else{
                            showToastMsg("网络加载出错了！")
                        }
                    }

                })
    }
    private inner class MyAdapter: RecyclerView.Adapter<MyHolder>(){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
            val view= LayoutInflater.from(parent.context).inflate(R.layout.rvitem_newmen_free, parent, false)
            return MyHolder(view)
        }

        override fun getItemCount(): Int {
            return goodsList.size
        }

        override fun onBindViewHolder(holder: MyHolder, position: Int) {
            if (goodsList.isNotEmpty()){
                val taobaoGoodBean=goodsList[position]
                holder.goodsTitle!!.text = taobaoGoodBean.title
                LoadUtils().loadRoundImag(this@BrandSpecialListActivity,holder.goodsImag!!,taobaoGoodBean.pict_url as String,R.drawable.ic_app_place_holder3,5)
                val price=taobaoGoodBean.final_price
                holder.tvXLNumber!!.text=taobaoGoodBean.volume
                holder.goodsOldPriceType!!.text="${taobaoGoodBean.goodsSource}价"
                holder.goodsOldPrice!!.text=taobaoGoodBean.original_price?:"0"
                holder.goodsFL!!.text="约返￥${taobaoGoodBean.vip_commission}"
                holder.goodsFK!!.text="券后￥"
                holder.tvPrice!!.text=price?:""
                holder.goodsYHQPrice!!.text=" ￥"+taobaoGoodBean.coupon_price?:"0"
                if (taobaoGoodBean.has_coupon){
                    holder.goodsYHQPrice!!.text=String.format("¥%s", taobaoGoodBean.coupon_price)
                    holder.couponLL!!.visibility= View.VISIBLE
                }else{
                    holder.couponLL!!.visibility= View.INVISIBLE
                }

                holder.itemView.setOnClickListener {
                    val intent = Intent(this@BrandSpecialListActivity, GoodDetailActivity::class.java)
                    intent.putExtra("good", taobaoGoodBean)
                    startActivity(intent)
                }
            }
        }

    }
    private inner class MyHolder(itemview: View): RecyclerView.ViewHolder(itemview){
        var goodsImag: ImageView?=null
        var goodsType:ImageView?=null
        var goodsTitle: TextView?=null
        var goodsOldPrice: TextView?=null
        var goodsOldPriceType: TextView?=null
        var goodsFL: TextView?=null
        var goodsFK: TextView?=null
        var goodsYHQPrice: TextView?=null
        var goodsXLNumber: TextView?=null
        var tvGoodsType: TextView?=null
        var tvPrice:TextView?=null
        var tvXLNumber:TextView?=null
        var couponLL: LinearLayout?=null
        init {
            goodsImag=itemView.ivGoodImage
            goodsTitle=itemView.tvGoodName
            goodsOldPrice=itemView.tvGoodSvipProfit
            goodsOldPriceType=itemView.tvGoodtype
            goodsFL=itemView.tvGoodVipProfit
            goodsFK=itemview.tvCouponPriceTitle
            goodsYHQPrice=itemView.tvCouponAmount
            goodsXLNumber=itemView.tvGoodVolume
            tvGoodsType=itemview.tvGoodtype
            tvPrice=itemview.tvPrice
            tvXLNumber=itemview.tvXLNumber
            couponLL=itemView.choiceness_rvitm_llq1
            goodsType=itemview.imagGoodstype
            goodsXLNumber!!.visibility=View.INVISIBLE
            goodsType!!.visibility=View.GONE
            goodsTitle!!.maxLines=1
            tvGoodsType!!.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG
            goodsOldPrice!!.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG
        }
    }
}